package com.itheima.search.service.impl;

import com.itheima.search.pojo.Question;
import com.itheima.search.repository.QuestionRepository;
import com.itheima.search.service.EsSearchService;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <dl>
 * <dd>描述: ~节点业务逻辑实现</dd>
 * <dd>创建时间：  15:33 2020/6/23</dd>
 * <dd>创建人： zz</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2020/6/23      guodong       1.0        1.0 Version
 * </pre>
 * </dl>
 */
@Service
public class EsSearchServiceImpl implements EsSearchService {

    @Autowired
    private QuestionRepository questionRepository;

    /**
    * @Date: 16:00 2020/6/23
    * @Param: [question]
    * @return: void
    * @Description: 新增问题
    **/
    @Override
    public void addQuestion(Question question) {
        questionRepository.save(question);
    }

    /**
    * @Date: 15:59 2020/6/23
    * @Param: [title]
    * @return: java.util.Map<java.lang.String,java.lang.Object>
    * @Description: 问题搜索
    **/
    @Override
    public Map<String, Object> search(String title) {
        Map<String, Object> resultMap = new HashMap<>();
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", title);
        Iterable<Question> questions = questionRepository.search(queryBuilder);
        resultMap.put("questionList", questions);
        return resultMap;
    }
}
